/*  
 * i-OSGi - Tunable Bundle Isolation for OSGi
 * Copyright (C) 2011  Sven Schulz
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.iosgi.impl.engine;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.jgrapht.Graph;
import org.jgrapht.VertexFactory;
import org.jgrapht.generate.GraphGenerator;

/**
 * @author Sven Schulz
 */
public class IndependentSetGraphGenerator<V, E> implements
		GraphGenerator<V, E, V> {

	private final int numberOfVertices;
	private final int numberOfIndependentSets;
	private final Random random;
	private final double iisep;

	/**
	 * @param numberOfVertices
	 * @param numberOfIndependentSets
	 * @param iisep
	 *            the probability that there is an edge between two vertices in
	 *            an independent set
	 * @param random
	 */
	public IndependentSetGraphGenerator(int numberOfVertices,
			int numberOfIndependentSets, double iisep, Random random) {
		this.numberOfVertices = numberOfVertices;
		this.numberOfIndependentSets = numberOfIndependentSets;
		this.iisep = iisep;
		this.random = random;
	}

	@Override
	public void generateGraph(Graph<V, E> graph, VertexFactory<V> factory,
			Map<String, V> resultMap) {

		/* Create the independent sets. */
		List<List<V>> isets = new ArrayList<List<V>>(numberOfIndependentSets);
		for (int i = 0; i < numberOfIndependentSets; i++) {
			isets.add(new LinkedList<V>());
		}

		/* Create vertices and assign randomly to one of the independent sets. */
		for (int i = 0; i < numberOfVertices; i++) {
			int c = random.nextInt(numberOfIndependentSets);
			V v = factory.createVertex();
			graph.addVertex(v);
			isets.get(c).add(v);
		}

		/* Create IS edges. */
		for (int i = 0; i < isets.size(); i++) {
			for (V v : isets.get(i)) {
				for (int j = 0; j < isets.size(); j++) {
					if (j == i)
						continue;
					for (V u : isets.get(j)) {
						graph.addEdge(v, u);
					}
				}
			}
		}

		/* Add random edges. */
		for (int i = 0; i < Math.pow(numberOfVertices, 2) * iisep; i++) {
			List<V> iset = isets.get(random.nextInt(isets.size()));
			int a = random.nextInt(iset.size());
			int b = random.nextInt(iset.size());
			if (a != b) {
				graph.addEdge(iset.get(a), iset.get(b));
			}
		}

	}

}